var page_box = document.getElementsByClassName('page_box')[0];
var page_title = page_box.getElementsByClassName('page_title')[0];
var page_classify = page_box.getElementsByClassName('page_classify')[0];
var page_content = page_box.getElementsByClassName('page_content')[0];
var page_time = page_box.getElementsByClassName('page_time')[0]

var submit_commend = document.getElementsByClassName('submit_commend')[0];
var submit = submit_commend.getElementsByClassName('submit')[0]
var input_commend = submit_commend.getElementsByClassName('input_commend')[0];

var commend_list_ul = document.getElementsByClassName('commend_list_ul')[0]

var parseUrl = function(str) {
    var arr = str.split('?')[1].split('&');
    var obj = {}
    arr.forEach((item) => {
        var arr1 = item.split('=')
        obj[arr1[0]] = arr1[1]
    })
    return obj
}
var { id } = parseUrl(location.href)

function renderGetOneArticleByIdData(result) {
    var item = JSON.parse(result)[0];
    page_title.innerHTML = item.title;
    page_content.innerHTML = item.content;
    page_classify.innerHTML = item.classify;
    page_time.innerHTML = item.time
}

function renderGetCommendByIdData(result) {
    commend_list_ul.innerHTML = ""
    var arr = JSON.parse(result)
    arr.forEach((item) => {
        var li = document.createElement('li');
        li.className = 'commend_item';
        li.innerHTML = item.commend;
        commend_list_ul.appendChild(li)
    })
}

async function handle() {
    var getOneArticleByIdData = await myAjax("GET", "/getOneArticleById?id=" + id, null)
    renderGetOneArticleByIdData(getOneArticleByIdData)
    var getCommendByIdData = await myAjax('GET', '/getCommendById?id=' + id, null)
    renderGetCommendByIdData(getCommendByIdData)
}
handle()

function renderSubmitCommendData(result) {
    alert(result)
    input_commend.value = "";
    async function handle() {
        var getCommendByIdData = await myAjax('GET', '/getCommendById?id=' + id, null)
        renderGetCommendByIdData(getCommendByIdData)
    }
    handle()
}

submit.addEventListener('click', function() {
    var value = input_commend.value;
    async function handle() {
        var submitCommendData = await myAjax("POST", '/submitCommend', { id: id, value: value })
        renderSubmitCommendData(submitCommendData)
    }
    handle()
})